﻿using AutoFlatnessTest.Db;
using Flurl;
using Flurl.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoFlatnessTest.Api
{
    public static class Cmcc
    {
        public static async Task<string> PlAddAsync(TestRecord testRecord)
        {
            try
            {
                var url = await ConModel.Data.Api
                    .AppendPathSegment("integrate/cmcc/plAdd")
                    .PostJsonAsync(new
                    {
                        columnNum = testRecord.ColumnNum ?? 0,
                        createdTime = (testRecord.CreatedTime.HasValue ? testRecord.CreatedTime.Value : DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss"),
                        devcode = ConModel.Data.DeviceCode,
                        guidId = testRecord.GuidId,
                        isOk = testRecord.IsOk ?? 0,
                        maInfo = testRecord.MaInfo,
                        miInfo = testRecord.MiInfo,
                        miMaxVal = testRecord.MiMaxVal,
                        miMinVal = testRecord.MiMinVal,
                        miSimi = testRecord.MiSimi,
                        miVal = testRecord.MiVal,
                        mode = testRecord.Mode,
                        projectId = testRecord.ProjectId,
                        projectName = testRecord.ProjectName,
                        rowNum = testRecord.RowNum ?? 0,
                        saveid = testRecord.Id,
                        testNum = testRecord.TestNum ?? 0,
                        workOrder = testRecord.WorkOrder,
                    })
                    .ReceiveJson();

                if (url.code == 200)
                    return string.Empty;
                else
                    return url.message;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public static async Task<string> PlcheckAddAsync(DemaRecord demaRecord)
        {
            try
            {
                var url = await ConModel.Data.Api
                .AppendPathSegment("integrate/cmcc/plcheckAdd")
                .PostJsonAsync(new
                {
                    createdTime = (demaRecord.CreatedTime.HasValue ? demaRecord.CreatedTime.Value : DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss"),
                    isOk = demaRecord.IsOk ?? 0,
                    miInfo = demaRecord.MiInfo,
                    miMaxVal = demaRecord.MiMaxVal ?? 0,
                    miMinVal = demaRecord.MiMinVal ?? 0,
                    miSimi = demaRecord.MiSimi ?? 0,
                    miVal = demaRecord.MiVal ?? 0,
                    testNum = demaRecord.TestNum ?? 0,
                })
                .ReceiveJson();

                if (url.code == 200)
                    return string.Empty;
                else
                    return url.message;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}
